/*
 Clientcide Copyright (c) 2006-2009, http://www.clientcide.com/wiki/cnet-libraries#license
 Contents: dbug, FixPNG
 This lib: http://www.clientcide.com/js/build.php?excludeLibs[]=mootools-core&excludeLibs[]=mootools-more&require[]=FixPNG&compression=none
*/
/*
 Script: dbug.js
 A wrapper for Firebug console.* statements.
 License: http://www.clientcide.com/wiki/cnet-libraries#license
*/
var dbug = {
  logged: [],
  timers: {},
  firebug: false,
  enabled: false,
  log: function() {
    dbug.logged.push(arguments);
  },
  nolog: function(msg) {
    dbug.logged.push(arguments);
  },
  time: function(name){
    dbug.timers[name] = new Date().getTime();
  },
  timeEnd: function(name){
    if (dbug.timers[name]) {
      var end = new Date().getTime() - dbug.timers[name];
      dbug.timers[name] = false;
      dbug.log('%s: %s', name, end);
    } else dbug.log('no such timer: %s', name);
  },
  enable: function(silent) {
    var con = window.firebug ? firebug.d.console.cmd : window.console;

    if((!!window.console && !!window.console.warn) || window.firebug) {
      try {
        dbug.enabled = true;
        dbug.log = function(){
            try {
              (con.debug || con.log).apply(con, arguments);
            } catch(e) {
              console.log(Array.slice(arguments));
            }
        };
        dbug.time = function(){
          con.time.apply(con, arguments);
        };
        dbug.timeEnd = function(){
          con.timeEnd.apply(con, arguments);
        };
        if(!silent) dbug.log('enabling dbug');
        for(var i=0;i<dbug.logged.length;i++){ dbug.log.apply(con, dbug.logged[i]); }
        dbug.logged=[];
      } catch(e) {
        dbug.enable.delay(400);
      }
    }
  },
  disable: function(){
    if(dbug.firebug) dbug.enabled = false;
    dbug.log = dbug.nolog;
    dbug.time = function(){};
    dbug.timeEnd = function(){};
  },
  cookie: function(set){
    var value = document.cookie.match('(?:^|;)\\s*jsdebug=([^;]*)');
    var debugCookie = value ? unescape(value[1]) : false;
    if((!$defined(set) && debugCookie != 'true') || ($defined(set) && set)) {
      dbug.enable();
      dbug.log('setting debugging cookie');
      var date = new Date();
      date.setTime(date.getTime()+(24*60*60*1000));
      document.cookie = 'jsdebug=true;expires='+date.toGMTString()+';path=/;';
    } else dbug.disableCookie();
  },
  disableCookie: function(){
    dbug.log('disabling debugging cookie');
    document.cookie = 'jsdebug=false;path=/;';
  }
};
(function(){
  var fb = !!window.console || !!window.firebug;
  var con = window.firebug ? window.firebug.d.console.cmd : window.console;
  var debugMethods = ['debug','info','warn','error','assert','dir','dirxml'];
  var otherMethods = ['trace','group','groupEnd','profile','profileEnd','count'];
  function set(methodList, defaultFunction) {
    for(var i = 0; i < methodList.length; i++){
      dbug[methodList[i]] = (fb && con[methodList[i]])?con[methodList[i]]:defaultFunction;
    }
  };
  set(debugMethods, dbug.log);
  set(otherMethods, function(){});
})();
if ((!!window.console && !!window.console.warn) || window.firebug){
  dbug.firebug = true;
  var value = document.cookie.match('(?:^|;)\\s*jsdebug=([^;]*)');
  var debugCookie = value ? unescape(value[1]) : false;
  if(window.location.href.indexOf("jsdebug=true")>0 || debugCookie=='true') dbug.enable();
  if(debugCookie=='true')dbug.log('debugging cookie enabled');
  if(window.location.href.indexOf("jsdebugCookie=true")>0){
    dbug.cookie();
    if(!dbug.enabled)dbug.enable();
  }
  if(window.location.href.indexOf("jsdebugCookie=false")>0)dbug.disableCookie();
}
/*
 Script: FixPNG.js
 Extends the Browser hash object to support PNG transparent images in MSIE 6.
 License: http://www.clientcide.com/wiki/cnet-libraries#license

 Note: This is a fork to be used with MooTools 1.3+ without retro-compatibility.
*/
Object.append(Browser, {
  fixPNG: function(el) {
    try {
      if (Browser.ie && (Browser.version < 7)) {
        el = document.id(el);
        if (!el) return el;
        if (el.get('tag') == "img" && el.get('src').test(".png")) {
          var vis = el.isDisplayed();
          try { //safari sometimes crashes here, so catch it
            dim = el.getSize();
          }catch(e){}
          if (!vis){
            var before = {};
            //use this method instead of getStyles
            ['visibility', 'display', 'position'].each(function(style){
              before[style] = this.style[style]||'';
            }, this);
            //this.getStyles('visibility', 'display', 'position');
            this.setStyles({
              visibility: 'hidden',
              display: 'block',
              position:'absolute'
            });
            dim = el.getSize(); //works now, because the display isn't none
            this.setStyles(before); //put it back where it was
            el.hide();
          }
          var replacement = new Element('span', {
            id:(el.id)?el.id:'',
            'class':(el.className)?el.className:'',
            title:(el.title)?el.title:(el.alt)?el.alt:'',
            styles: {
              display: vis?'inline-block':'none',
              width: dim.x,
              height: dim.y,
              filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader (src='"
                + el.src + "', sizingMethod='scale');"
            },
            src: el.src
          });
          if (el.style.cssText) {
            try {
              var styles = {};
              var s = el.style.cssText.split(';');
              s.each(function(style){
                var n = style.split(':');
                styles[n[0]] = n[1];
              });
              replacement.setStyle(styles);
            } catch(e){ dbug.log('fixPNG1: ', e)}
          }
          if (replacement.cloneEvents) replacement.cloneEvents(el);
          replacement.replaces(el);
        } else if (el.get('tag') != "img") {
          var imgURL = el.getStyle('background-image');
          if (imgURL.test(/\((.+)\)/)){
            el.setStyles({
              background: '',
              filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src=" + imgURL.match(/\((.+)\)/)[1] + ")"
            });
          };
        }
      }
    } catch(e) {dbug.log('fixPNG2: ', e)}
  },
  pngTest: /\.png$/, // saves recreating the regex repeatedly
  scanForPngs: function(el, className) {
    className = className||'fixPNG';
    //TODO: should this also be testing the css background-image property for pngs?
    //Q: should it return an array of all those it has tweaked?
    if (document.getElements){ // more efficient but requires 'selectors'
      el = document.id(el||document.body);
      el.getElements('img[src$=.png]').addClass(className);
    } else { // scan the whole page
      var els = $$('img').each(function(img) {
        if (Browser.pngTest(img.src)){
          img.addClass(className);
        }
      });
    }
  }
});
if (Browser.ie && (Browser.version < 7)) {
  window.addEvent('domready', function() {
    $$(document.body, 'body *').each(
      function(elm) {
        if (!elm.hasClass('fixPNG')) {
          if (elm.nodeName == 'IMG') {
            if (!Browser.pngTest(elm.src)) return;
          } else {
            var imgURL = elm.getStyle('background-image');
            if (!imgURL || !/\(.+?\.png\)$/i.test(imgURL)) return;
          }
        }
        Browser.fixPNG(elm);
      });
  });
}